<?php

/**
 * @content: 获取标本检查列表--用于Android端的service同步数据
 * @Author:  Groube
 * @Date:    2017-03-10 09:30:00
 * @version  $Id$
 */
class GetjianchadatajsonController extends Yaf_Controller_Abstract {

    /**
     * @Title  禁用模板功能
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/10 09:30
     */
    public function init() {
        $yiyuan_id = isset($_REQUEST['yiyuan_id']) ? $_REQUEST['yiyuan_id'] : "";

        if($yiyuan_id === "" ){
            Utility::apiJsonError(20028, '请输入医院ID');
        }elseif(!ctype_digit("$yiyuan_id")){
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }

        Yaf_Dispatcher::getInstance()->disableView();
    }


    /**
     * @Title  入口方法
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/10 09:30
     */
    function indexAction(){
        $request = $this->getRequest();
        $data = $request->getPost('data','');
        $user_department = $request->getPost('user_department', '');
        $modify_time = $request->getPost('modify_time', '');
        $str_yiyuan_id = $request->getPost('yiyuan_id',0);
        $yiyuan_id = (int)$str_yiyuan_id;

        if(empty($user_department)){
            Utility::apiJsonError(40020,'(Method:获取标本检查列表)科室不能为空!');
        }

        /*是否开启压缩 默认开启*/
        $compression_http_data = $request->getPost('compression_http_data','off');

        //解压缩数据
        if(!empty($data)){
            $upload_data = json_decode(Utility::uncompress($data,$compression_http_data),true);

            if(empty($upload_data)){
                $upload_data = '';
                //Utility::apiJsonError(40021,'(Method:获取标本检查列表)数据解析有误!');
            }
        }else{
            $upload_data = '';
        }


        //获取标本信息
        $async_result = $this->getBiaobenSql($yiyuan_id,$user_department,$upload_data,$modify_time);
        if(empty($async_result)){
            $data = array('error'=>200,'msg'=>'暂无数据!','update_sql' => '');
            Utility::apiJsonResultExtends($data);
        }

        $params_mod = new ZhuyuanFuzhuJianchaModel;
        $field_arr = $params_mod->_biaoben_field_arr;
        $async_result = Utility::formatToSqlStr($async_result, 'zhuyuan_fuzhujiancha', 'update_sql', $field_arr);
        $async_result['error'] = 200;
        $async_result['msg'] = '';
        Utility::apiJsonResultExtends($async_result);
    }


    public function getBiaobenSql($yiyuan_id,$user_department,$upload_data,$modify_time = ''){
        $zhuyuan_basic_info = DB::select('zhuyuan_id')->from('zhuyuan_basic_info')
            ->where('zhuyuan_bingqu','=',$user_department)
            ->where('zhuangtai','=','住院中')
            ->where('yiyuan_id','=',$yiyuan_id)
            ->execute()
            ->as_array();

        $zhuayun_bingren = array();
        if(!empty($zhuyuan_basic_info)){
            foreach($zhuyuan_basic_info as $one){
                if(!in_array($one['zhuyuan_id'],$zhuayun_bingren)){
                    $zhuayun_bingren[] = $one['zhuyuan_id'];
                }
            }
        }else{
            Utility::apiJsonError(40022,'(Method:获取标本检查列表)住院基本信息表中没有查到数据!');
        }

        $params_mod = new ZhuyuanFuzhuJianchaModel;
        return $params_mod->ls_biaoben($zhuayun_bingren,$yiyuan_id,$upload_data,$modify_time);
    }
}